//
//  TabBarController.m
//  Graduate
//
//  Created by xp on 16/8/2.
//  Copyright © 2016年 mingthink. All rights reserved.
//

#import "TabBarController.h"
#import "MyUtility.h"
#import "TrendPageController.h"
#import "TradePageController.h"
#import "WebViewVC.h"
#import "AskDoctorVC.h"
#import "ConsultTreatVC.h"
#import "HomeVC.h"
#import "MapVC.h"
#import "MeVC.h"

//两次提示的默认间隔
static const CGFloat kDefaultPlaySoundInterval = 3.0;
static NSString *kMessageType = @"MessageType";
static NSString *kConversationChatter = @"ConversationChatter";
static NSString *kGroupName = @"GroupName";

@interface TabBarController ()<UITabBarControllerDelegate>

@property (strong, nonatomic) NSDate *lastPlaySoundDate;

@end

@implementation TabBarController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //self.tabBar.hidden = false;
    //处理点击通知消息启动app跳转指定页面
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRemotePush:) name:@"APNSJUMP" object:nil];
    self.navigationController.navigationBarHidden = true;
    self.tabBar.barTintColor=GET_HEX_COLOR(COLOR_TABBAR_BG);//更改背景颜色
    self.delegate = self;
    //更改tabBar顶部线条的高度和颜色
//    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.tabBar.height);
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
//    CGContextFillRect(context, rect);
//    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    [self.tabBar setBackgroundImage:img];
//    [self.tabBar setShadowImage:img];
    //添加顶部黑色的线
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, [UIScreen mainScreen].bounds.size.width, 0.5)];
    [self.tabBar addSubview:line];
    line.backgroundColor = GET_HEX_COLOR(SCREEN_BG_COLOR);

    //DeLog(@"------%@",pageController.tabBarItem);
    //[pageController showRightBtnWithImage:@"r_gray_search"];

    NSArray *childItems = @[
                            @{@"class"  :AskDoctorVC.class,
                              @"title"  : @"求医",
                              @"unSelectImage"    : @"tabbar_1_normal",
                              @"selectImage" : @"tabbar_1_press",
                              @"url"    :  @"Index/index"},
                            
                            @{@"class"  : ConsultTreatVC.class,
                              @"title"  : @"问诊",
                              @"unSelectImage"    : @"tabbar_2_normal",
                              @"selectImage" : @"tabbar_2_press",
                              @"url"    :  @"Article/index/id/1"},
                            
                            @{@"class"  : HomeVC.class,
                              @"title"  : @"首页",
                              @"unSelectImage"    : @"tabbar_3_normal",
                              @"selectImage" : @"tabbar_3_press",
                              @"url"    :  @"Trade/index/id/1"},
                            
                            @{@"class"  : MapVC.class,
                              @"title"  : @"地图",
                              @"unSelectImage"    : @"tabbar_4_normal",
                              @"selectImage" : @"tabbar_4_press",
                              @"url"    :  @"Trade/index/id/1"},
                            
                            @{@"class"  : MeVC.class,
                              @"title"  : @"我",
                              @"unSelectImage"    : @"tabbar_5_normal",
                              @"selectImage" : @"tabbar_5_press",
                              @"url"    :  @"User/Index"}
                            ];
    for (int i=0;i<childItems.count;i++) {
        NSDictionary *dict=childItems[i];
        UIViewController *vc = nil;
            vc=[dict[@"class"] new];
        //vc.title = dict[@"title"];
        //vc.hidesBottomBarWhenPushed = true;
        
//        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
//        nav.navigationBarHidden = YES;
        UITabBarItem *item = vc.tabBarItem;
        item.title = dict[@"title"];
        item.image = [[UIImage imageNamed:dict[@"unSelectImage"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.selectedImage = [[UIImage imageNamed:dict[@"selectImage"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [item setTitleTextAttributes:@{NSFontAttributeName:GET_SYSTEM_FONT(TEXT_FONT_20)} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName : GET_HEX_COLOR(COLOR_TABBAR_TEXT)} forState:UIControlStateSelected];
        item.titlePositionAdjustment = UIOffsetMake(0, -3);
        item.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0);
        [self addChildViewController:vc];
        
//        if ([dict[@"title"] isEqualToString:@"测"]){
//            item.title = @"";
//        }
    }

    //  指定当前页——中间页
    self.selectedIndex = 0;
}
//通知中心
-(void)showOrHiddenTabBar:(NSNotification*)notice{
    NSDictionary *dic = notice.object;
    if ([dic[@"isShow"] isEqualToString:@"true"]){
        self.tabBar.hidden = false;
    }else{
        self.tabBar.hidden = true;
    }
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    DeLog(@"------index1:%ld",self.selectedIndex);
//    NSDictionary *infoDict=@{@"index":[NSString stringWithFormat:@"%ld",self.selectedIndex]};
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"TabBarClickNotification" object:nil userInfo:infoDict];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        NSDictionary *infoDict=@{@"index":[NSString stringWithFormat:@"%ld",self.selectedIndex]};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TabBarClickNotification" object:nil userInfo:infoDict];
       DeLog(@"------index3:%ld",self.selectedIndex);
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
        DeLog(@"------index2:%ld",self.selectedIndex);
//    UINavigationController *nav = (UINavigationController*)viewController;
//    NSArray *vcs = nav.viewControllers;
//    int index = self.tabBarController.selectedIndex;
    
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
